<?php
class Fang_DAO_Photo extends DAO_Mysql {

    const STATUS_NORMAL = 1;

    const STATUS_REMOVE = 9;

    private static $_table = 'fang_photos';

    /**
     * add 
     * 
     * @param  mixed $data 
     * @return void
     */
    public static function add($data) {

        if (!$data['p_created']) {
            $data['p_created'] = date('Y-m-d H:i:s');
        }

        if (!$data['p_status']) {
            $data['p_status'] = self::STATUS_NORMAL;
        }

        return parent::add(self::$_table, $data);
    }

    /**
     * update 
     * 
     * @param  mixed $pid 
     * @param  mixed $data 
     * @return void
     */
    public static function update($pid, $data) {

        if (!$data['p_updated']) {
            $data['p_updated'] = date('Y-m-d H:i:s');
        }

        return parent::updateByKey(self::$_table, $data, $pid);
    }

    /**
     * remove 
     * 
     * @param  mixed $pid 
     * @return void
     */
    public static function remove($pid) {

        if (!$data['p_deleted']) {
            $data['p_deleted'] = date('Y-m-d H:i:s');
        }

        if (!$data['p_status']) {
            $data['p_status'] = self::STATUS_REMOVE;
        }

        return self::update($pid, $data);
    }

    /**
     * getInfo 
     * 
     * @param  mixed $pid 
     * @param  array $fields 
     * @return void
     */
    public static function getInfo($pid, $fields = array()) {

        $conditions = 'pid = ? AND p_status != ?';
        $params = array($pid, self::STATUS_REMOVE);
        return parent::getRow(self::$_table, $conditions, $params, $fields);
    }

    /**
     * updateByPids 
     * 
     * @param  mixed $pids 
     * @param  mixed $data 
     * @return void
     */
    public static function updateByPids($pids, $data) {

        $params = array();
        $conditions = 'pid IN (' . parent::buildInSQLClause($pids, $params) . ') AND p_status != ?';
        $params[] = self::STATUS_REMOVE;

        return parent::update(self::$_table, $data, $conditions, $params);
    }

    /**
     * listByPids 
     * 
     * @param  mixed $pids 
     * @return void
     */
    public static function listByPids($pids) {

        $params = array();
        $conditions = 'pid IN (' . parent::buildInSQLClause($pids, $params) . ') AND p_status != ?';
        $params[] = self::STATUS_REMOVE;

        return parent::getAll(self::$_table, $conditions, $params);
    }

    /**
     * listByAid 
     * 
     * @param  mixed $aid 
     * @param  array $fields 
     * @return void
     */
    public static function listByAid($aid, $fields = array()) {

        $conditions = 'aid = ? AND p_status != ?';
        $params = array($aid, self::STATUS_REMOVE);
        return parent::getAll(self::$_table, $conditions, $params, $fields);
    }

    /**
     * listAll 
     * 
     * @param  array $pageOptions 
     * @param  array $fields 
     * @return void
     */
    public static function listAll($pageOptions = array(), $fields = array()) {

        return parent::getAll(self::$_table, null, null, $fields, $pageOptions, array('pid' => 'DESC'));
    }

    /**
     * countAll 
     * 
     * @return void
     */
    public static function countAll() {

        return parent::count(self::$_table);
    }
}
